<?php
class Mysql {
	public $conn;
	public $prefix;
	function __construct($host, $name, $pass, $datebase, $prefix){
		$this->conn = mysql_connect($host, $name, $pass, true) or die(mysql_error());
		$this->prefix = $prefix;
		mysql_select_db($datebase);
		mysql_query("SET NAMES UTF8", $this->conn);
	}
	
	function query($sql){
		return mysql_query($sql, $this->conn);
	}
	
	function unbuffered_query($sql){
		return mysql_unbuffered_query($sql, $this->conn);
	}
	
	function fetchAll($sql){
		$res = $this->unbuffered_query($sql);
		$rows = array();
		while ($row = mysql_fetch_assoc($res)){
			$rows[] = $row;
		}
		return $rows;
	}
	
	function fetchFirst($sql){
		$res = $this->unbuffered_query($sql);
		return mysql_fetch_assoc($res);
	}
	
	function insert_id(){
		return mysql_insert_id($this->conn);
	}
	
	function insert($tablename, $arr){
		$set = '';
		foreach((array)$arr as $k => $v){
			$set .= '`'.$k.'` = \''.$v.'\', ';
		}
		$set = substr($set, 0, -2);
		$sql = 'INSERT INTO `'.$this->prefix.$tablename.'` SET '.$set;
		return $this->query($sql);
	}
	
	function replace_into($tablename, $arr){
		$set = '';
		foreach((array)$arr as $k => $v){
			$set .= '`'.$k.'` = \''.$v.'\', ';
		}
		$set = substr($set, 0, -2);
		$sql = 'REPLACE INTO `'.$this->prefix.$tablename.'` SET '.$set;
		return $this->query($sql);
	}
    
    function get_server_info(){
        return mysql_get_server_info($this->conn);
    }
	
	function close(){
		return mysql_close($this->conn);
	}
}
